//
//  WmcCalendarSeleceCell.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/3/31.
//

import FSCalendar
import UIKit

class WmcCalendarSeleceCell: FSCalendarCell {
    // Lifecycle

    override init!(frame: CGRect) {
        super.init(frame: frame)
        makeUI()
    }

    required convenience init!(coder aDecoder: NSCoder!) {
        self.init(frame: .zero)
    }

    // Internal

    func makeUI() {
        contentView.addSubview(bgView)
        bgView.addSubview(dateLabel)
        bgView.addSubview(pointView)
        
        bgView.backgroundColor = .wmc_blue_EDF3FE
        
        bgView.snp.makeConstraints { make in
            make.left.top.equalTo(1)
            make.right.bottom.equalTo(-1)
        }
        
        dateLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.right.equalToSuperview()
            make.height.equalTo(20)
        }
        
        pointView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(dateLabel.snp.bottom).offset(5)
            make.width.height.equalTo(4)
        }
    }
    
    private lazy var bgView: UIView = {
        let view = UIView()
        view.layer.cornerRadius = 2
        return view
    }()
    
    private lazy var dateLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 18, weight: .medium)
        label.textColor = .wmc_grayH2_323233
        label.textAlignment = .center
        return label
    }()
    
    private lazy var pointView: UIView = {
        let view = UIView()
        view.backgroundColor = .wmc_mainTheme
        view.layer.cornerRadius = 2
        view.isHidden = true
        return view
    }()
    
    func update(date: Date, position: FSCalendarMonthPosition, isContainData: Bool, isSelected: Bool) {
        dateLabel.text = date.yp.string("dd")
        
        if isSelected {
            bgView.backgroundColor = .wmc_mainTheme
        } else if isContainData {
            bgView.backgroundColor = .wmc_blue_EDF3FE
        } else {
            bgView.backgroundColor = .clear
        }
        
        var currentDate = Date()
        if let dateTemp = WmcDateManager.shared.networkDate.value {
            currentDate = dateTemp - 8.yp.hour
            
            if let dateTemp = currentDate.yp.string(.iso8601Day).yp.date(format: .iso8601Day) {
                currentDate = dateTemp
            }
        }
        
        var calendarDate = date
        if let dateTemp = date.yp.string(.iso8601Day).yp.date(format: .iso8601Day) {
            calendarDate = dateTemp
        }
        
        if position == .current {
            pointView.isHidden = true
            
            if calendarDate.timeIntervalSince1970 > currentDate.timeIntervalSince1970 {
                dateLabel.textColor = .wmc_grayH14_CCCCCC
            } else if calendarDate.timeIntervalSince1970 == currentDate.timeIntervalSince1970 {
                pointView.isHidden = false
                dateLabel.textColor = isSelected ? .wmc_white_FFFFFF : .wmc_mainTheme
            } else if isSelected {
                dateLabel.textColor = .wmc_white_FFFFFF
            } else {
                dateLabel.textColor = .wmc_grayH2_323233
            }
        } else {
            dateLabel.textColor = .wmc_grayH14_CCCCCC
        }
    }
}
